﻿/*
   Open Intel 
   Copyright © 2011 – ISC. All Rights Reserved.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Media;
using ISC.MapDotNetServer.Common;
using ISC.MapDotNetServer.Common.Maps;
using ISC.RIM.Silverlight.Projections;

namespace OI.Framework
{
    public static class Utility
    {
        public static string TruncateOnWordBoundary(this string str, int maxChars)
        {
            if (!string.IsNullOrEmpty(str))
            {
                char[] chars = { ' ' };
                string[] words = str.Split(chars, StringSplitOptions.RemoveEmptyEntries);

                var sb = new StringBuilder();
                int count = 0;

                foreach (var word in words)
                {
                    count += (word.Length + 1);
                    if (count > maxChars)
                    {
                        sb.Append("...");
                        break;
                    }
                    sb.Append(word + ' ');
                }
                return sb.ToString();
            }
            return "";
        }


        public static Color GetColorFromHex(string hexColor)
        {
            return Color.FromArgb(
                    Convert.ToByte(hexColor.Substring(1, 2), 16),
                    Convert.ToByte(hexColor.Substring(3, 2), 16),
                    Convert.ToByte(hexColor.Substring(5, 2), 16),
                    Convert.ToByte(hexColor.Substring(7, 2), 16));
        }


        public static bool IsValidStreetAddress(string address)
        {
            const string exp = @"^\d+\s+[a-zA-Z0-9]+\s+[a-zA-Z0-9]{2,}";
            var regex = new Regex(exp);
            return regex.IsMatch(address.Trim());
        }


        // spherical mercator coordinate system
        public static ProjCoordSys SphericalMercatorCoordSys
        {
            get
            {
                return _SphericalMercatorCoordSys ?? (_SphericalMercatorCoordSys = new ProjCoordSys
                {
                    CSString = SphericalMercatorProj.GetProjCSString(),
                    ID = SphericalMercatorProj.GetEPSG()
                });
            }
        }
        private static ProjCoordSys _SphericalMercatorCoordSys;


        // spherical mercator ITileProjection
        public static SphericalMercatorTileProjection SphericalMercatorProj
        {
            get 
            { 
                return _SphericalMercatorProj ?? (_SphericalMercatorProj = new SphericalMercatorTileProjection()); 
            }
        }
        private static SphericalMercatorTileProjection _SphericalMercatorProj;


        public static bool AreMouseHoverEventsEnabled(this Map map)
        {
            if (map.MetaData.ContainsKey(Configuration.EnableMouseHoverEventsMetaKey))
            {
                string val = map.MetaData[Configuration.EnableMouseHoverEventsMetaKey].ToLower();
                return Convert.ToBoolean(val);
            }
            return false;
        }


        #region extension methods on Layer
        public static bool DrilldownIgnore(this Layer layer)
        {
            if (layer.MetaData.ContainsKey(Configuration.DrilldownIgnoreMetaKey))
            {
                string val = layer.MetaData[Configuration.DrilldownIgnoreMetaKey].ToLower();
                return Convert.ToBoolean(val);
            }
            return false;
        }


        public static bool HasDensityMapping(this Layer layer)
        {
            return layer.MetaData.ContainsKey(Configuration.DensityMappingMetaKey);
        }


        public static string GetDensityMappingString(this Layer layer)
        {
            return layer.MetaData.ContainsKey(Configuration.DensityMappingMetaKey) ? 
                layer.MetaData[Configuration.DensityMappingMetaKey] : 
                "";
        }


        public static void SetDensityMappingString(this Layer layer, string val)
        {
            layer.MetaData[Configuration.DensityMappingMetaKey] = val;
        }


        public static bool HasDynamicLabeling(this Layer layer)
        {
            return layer.MetaData.ContainsKey(Configuration.DynamicLabelingMetaKey);
        }


        public static string GetDynamicLabelingString(this Layer layer)
        {
            return layer.MetaData.ContainsKey(Configuration.DynamicLabelingMetaKey) ?
                layer.MetaData[Configuration.DynamicLabelingMetaKey] :
                "";
        }


        public static void SetDynamicLabelingString(this Layer layer, string val)
        {
            layer.MetaData[Configuration.DynamicLabelingMetaKey] = val;
        }


        public static bool HasDrilldownHeaders(this Layer layer)
        {
            return layer.MetaData.ContainsKey(Configuration.DrilldownHeadersMetaKey);
        }


        public static string GetDrilldownHeaders(this Layer layer)
        {
            return layer.MetaData.ContainsKey(Configuration.DrilldownHeadersMetaKey) ?
                layer.MetaData[Configuration.DrilldownHeadersMetaKey] :
                "";
        }


        public static bool HasDrilldownColumns(this Layer layer)
        {
            return layer.MetaData.ContainsKey(Configuration.DrilldownColumnsMetaKey);
        }


        public static string GetDrilldownColumns(this Layer layer)
        {
            return layer.MetaData.ContainsKey(Configuration.DrilldownColumnsMetaKey) ?
                layer.MetaData[Configuration.DrilldownColumnsMetaKey] :
                "";
        }


        public static string GetDrilldownPhotoColumn(this Layer layer)
        {
            return layer.MetaData.ContainsKey(Configuration.DrilldownPhotoColumnMetaKey) ?
                layer.MetaData[Configuration.DrilldownPhotoColumnMetaKey] :
                "";
        }


        public static bool HasSearchColumns(this Layer layer)
        {
            return layer.MetaData.ContainsKey(Configuration.SearchColumnsMetaKey);
        }


        public static string GetSearchColumns(this Layer layer)
        {
            return layer.MetaData.ContainsKey(Configuration.SearchColumnsMetaKey) ?
                layer.MetaData[Configuration.SearchColumnsMetaKey] :
                "";
        }

        public static Dictionary<string, string> GetHyperlinkColumns(this Layer layer)
        {
            var list = new Dictionary<string, string>();

            if (layer.MetaData.ContainsKey(Configuration.HyperlinkColumnsMetaKey))
            {
                var data = layer.MetaData[Configuration.HyperlinkColumnsMetaKey];
                var hs = data.Split('$');
                foreach (var t in hs.Select(h => h.Split(',')).Where(t => t.Length == 2))
                {
                    list.Add(t[0], t[1]);
                }
            }
            return list;
        }


        public static string GetLayerName(this Layer layer, string defaultName)
        {
            return layer.MetaData.ContainsKey(Configuration.LayerNameMetaKey) ?
               layer.MetaData[Configuration.LayerNameMetaKey] :
               defaultName;
        }


        public static bool HasRSSEnable(this Layer layer)
        {
            return layer.MetaData.ContainsKey(Configuration.RSSEnableMetaKey);
        }


        public static bool HideLayersOutOfScale(this Layer layer)
        {
            return layer.MetaData.ContainsKey(Configuration.HideLayersOutOfScale) ?
                Convert.ToBoolean(layer.MetaData[Configuration.HideLayersOutOfScale]) :
                false;
        }

        #endregion
    }
}
